package com.itheima.service.impl;

import com.itheima.dao.CheckGroupMapper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.dao.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 获取套餐列表
     *
     * @return
     */
    public List<Setmeal> getSetmeal() {
        return setmealMapper.getSetmeal();
    }

    /**
     * 根据套餐id查询套餐
     *
     * @param id
     * @return
     */
    public Setmeal findById(Integer id) {
        // 根据套餐 ID 查询套餐信息
        Setmeal setmeal = setmealMapper.findById(id);
        List<CheckGroup> checkGroups = new LinkedList<CheckGroup>();
        if (setmeal == null) {
            return null;
        }
        List<Integer> checkGroupIds = setmealMapper.findCheckGroupIdsBySetmealId(id);
        if (!checkGroupIds.isEmpty() && checkGroupIds != null) {
            for (Integer checkGroupId : checkGroupIds) {
                CheckGroup checkGroup = checkGroupMapper.findById(checkGroupId);
                checkGroups.add(checkGroup);
                List<CheckItem> checkItems = new LinkedList<CheckItem>();
                List<Integer> checkItemIds = checkGroupMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
                if (!checkItemIds.isEmpty() && checkItemIds != null) {
                    for (Integer checkItemId : checkItemIds) {
                        CheckItem checkItem = checkItemMapper.findCheckItemById(checkItemId);
                        checkItems.add(checkItem);
                    }
                }
                checkGroup.setCheckItems(checkItems);

            }
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }
}